package com.qf.agro.service.device.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.sys.SysUserHolder;
import com.qf.agro.mapper.device.SmartDeviceLogMapper;
import com.qf.agro.mapper.device.SmartDeviceMapper;
import com.qf.agro.pojo.device.SmartDevice;
import com.qf.agro.pojo.device.SmartDeviceLog;
import com.qf.agro.pojo.sys.SysUser;
import com.qf.agro.service.device.SmartDeviceService;
import com.qf.agro.vo.OptionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @company：千锋教育
 * @author：zhy
 * @date：2023/7/2
 */
@Service
public class SmartDeviceServiceImpl implements SmartDeviceService {

    @Resource
    private SmartDeviceMapper smartDeviceMapper;

    @Resource
    private SmartDeviceLogMapper smartDeviceLogMapper;

    @Override
    public Page<SmartDevice> findPage(Integer pageNum, Integer pageSize, String name, String facilitiesId) {
        //1. 创建查询条件对象
        LambdaQueryWrapper<SmartDevice> wrapper = new LambdaQueryWrapper<>();
        //2.设置查询条件
        wrapper.like(StringUtils.isNotEmpty(name), SmartDevice::getDeviceName, name);//where name like %#{name}%
        wrapper.eq(StringUtils.isNotEmpty(facilitiesId), SmartDevice::getFacilitiesId, facilitiesId);//where name like %#{name}%

        wrapper.orderByDesc(SmartDevice::getId);
        //3 设置分页条件
        Page<SmartDevice> page = new Page<>(pageNum, pageSize);
        //4 查询
        page = smartDeviceMapper.selectPage(page, wrapper);
        //5 返回
        return page;
    }

    @Override
    public void save(SmartDevice smartDevice) {
        smartDeviceMapper.insert(smartDevice);
    }

    @Override
    public void update(SmartDevice smartDevice) {
        smartDeviceMapper.updateById(smartDevice);
    }

    @Override
    public void delete(Long id) {
        smartDeviceMapper.deleteById(id);
    }

    @Override
    public SmartDevice findById(Long id) {
        return smartDeviceMapper.selectById(id);
    }

    @Override
    public List<OptionVo> selectOptions(String facilitiesIdSelect) {
        return smartDeviceMapper.selectOptions(facilitiesIdSelect);
    }

    @Override
    public List<SmartDevice> selectByFacilitiesId(String facilitiesId) {
        LambdaQueryWrapper<SmartDevice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmartDevice::getFacilitiesId,facilitiesId);
        queryWrapper.eq(SmartDevice::getCompanyId, SysUserHolder.get().getCompanyId());
        return smartDeviceMapper.selectList(queryWrapper);
    }

    @Override
    public int updateStateById(Long deviceId) {
        LambdaQueryWrapper<SmartDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmartDevice::getId,deviceId);
        SmartDevice smartDevice = smartDeviceMapper.selectOne(wrapper);
        SmartDeviceLog log = new SmartDeviceLog();
        SysUser sysUser = SysUserHolder.get();
        if (smartDevice.getState()==1){
            smartDevice.setState(0);
            log.setCode("关闭设备");
        }else {
            smartDevice.setState(1);
            log.setCode("打开设备");
        }
        log.setDeviceId(Long.valueOf(deviceId));
        log.setDeviceName(smartDevice.getDeviceName());
        log.setFacilitiesId(smartDevice.getFacilitiesId());
        log.setFacilitiesName(smartDevice.getFacilitiesName());
        log.setUserId(Math.toIntExact(sysUser.getId()));
        log.setSendTime(new Date());
        log.setCompanyId(Math.toIntExact(sysUser.getCompanyId()));
        log.setCompanyName(sysUser.getCompanyName());
        smartDeviceLogMapper.insert(log);
        return smartDeviceMapper.updateById(smartDevice);
    }
}
